package study.oop;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import study.enums.OrgType;
import study.interfaces.IBaseEnum;

import java.lang.reflect.Method;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Cell {

    private IBaseEnum data;
    private String stringValue;

    @SneakyThrows
    public IBaseEnum getData() {
        String value = this.getStringValue();

        Class clazz = OrgType.class;
        Object[] enumConstants = clazz.getEnumConstants();
        Method getDesc = clazz.getMethod("getDesc");
        for (Object enumConstant : enumConstants) {
            if (getDesc.invoke(enumConstant).equals(value)) {
                return (IBaseEnum) enumConstant;
            }
        }

        return null;
    }

    public Cell convertToExcelData(IBaseEnum iBaseEnum) {
        return new Cell(iBaseEnum, iBaseEnum.getDesc());
    }
}
